package com.kuaimacode.kframework.admin.services.common;

import com.kuaimacode.kframework.util.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.admin.services.common
 * @Description: created by IntelliJ IDEA
 * @date 2019-04-13 11:14
 */
@Service
public class UploadService {

    @Value("${img.upload.location.root}")
    private String uploadRootLocation;
    @Value("${img.upload.location.admin.img}")
    private String imgUploadLocation;

    public String uploadFile(MultipartFile file) throws IOException {
        String fileName = imgUploadLocation + DateUtil.getNow(DateUtil.FORMAT_FOUR_MINUTE) + file.getOriginalFilename();
        File destFile = new File(uploadRootLocation + fileName);
        destFile.getParentFile().mkdirs();
        //5.把浏览器上传的文件复制到希望的位置
        file.transferTo(destFile);
        return fileName;
    }
}
